package 力扣面试经典150;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2024-04-14
 * Time:16:08
 */

/**
 * 力扣面试经典150:17. 电话号码的字母组合
 * 中等
 * 给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
 * 给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。
 * 示例 1：
 * 输入：digits = "23"
 * 输出：["ad","ae","af","bd","be","bf","cd","ce","cf"]
 */
public class letterCombinations {
    //保存按键对应的字母
    String[] hash = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
    List<String> res;
    StringBuffer str;
    public List<String> letterCombinations(String digits) {
        res = new ArrayList<>();
        str = new StringBuffer();
        if(digits.length() == 0)return res;
        dfs(digits,0);
        return res;
    }
    public void dfs(String digits,int index){
        if(index == digits.length()){
            res.add(str.toString());
            return;
        }
        //curStr 为当前对应按键上的字母
        String curStr = hash[digits.charAt(index) - '0'];
        for(int i = 0;i < curStr.length();i++){
            str.append(curStr.charAt(i));
            dfs(digits,index + 1);
            str.deleteCharAt(str.length() - 1);
        }
    }
}
